package com.example.demo.admin.test.concurrent;/**
 * Created by kete on 2018-09-27.
 */

/**
 * 作者：kete
 * 创建时间：2018-09-27 17:10
 * 功能描述：
 * 版本：
 */
public class OneThread {
    private int count =1;

    public static void main(String[] args) {
        OneThread thread = new OneThread();
        thread.out();
    }

    public void out(){
        new Thread(()->{
            for(int i =0 ; i<100; i++){
                print1();
            }

        },"thread1").start();

        new Thread(()->{
            for(int i =0 ; i<100; i++){
                print2();
            }

        },"thread1").start();
    }

    public synchronized void print1(){
        while (count%2==0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Thread Name :"+Thread.currentThread().getName()+" ji:"+count++);
        this.notify();
    }

    public synchronized void print2(){
        while (count%2==1){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Thread Name :"+Thread.currentThread().getName()+" ou:"+count++);
        this.notify();
    }

}
